package com.bdqn.easybuy1.dao.impl;

import com.bdqn.easybuy1.dao.BaseDao;
import com.bdqn.easybuy1.dao.NewsDao;
import com.bdqn.easybuy1.entities.News;
import com.bdqn.easybuy1.param.NewsParams;
import com.bdqn.easybuy1.utils.EmptyUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl extends BaseDao<News> implements NewsDao {
    /**
     * 往数据库中插入一条新闻信息
     *
     * @param news 保存新闻信息的对象
     * @return 受影响的行数
     * @throws Exception
     */
    @Override
    public int insert(Connection conn, News news) throws Exception {
        String sql = "INSERT INTO `easybuy_news`(`title`, `content`, `createTime`) VALUES(?,?,?)";
        return super.update(conn, sql, news.getTitle(), news.getContent(), news.getCreateTime());
    }

    /**
     * 修改新闻
     *
     * @param news 保存新闻信息的对象
     * @return 受影响的行数
     * @throws Exception
     */
    @Override
    public int update(Connection conn, News news) throws Exception {
        String sql = "UPDATE `easybuy_news` SET `title` = ?, `content` = ? WHERE `id` = ?";
        return super.update(conn, sql, news.getTitle(), news.getContent(), news.getId());
    }

    /**
     * 根据id删除新闻
     *
     * @param id 要删除的新闻ID
     * @return 受影响的行数
     * @throws Exception
     */
    @Override
    public int deleteById(Connection conn, Integer id) throws Exception {
        String sql = "DELETE FROM `easybuy_news` WHERE `id` = ?";
        return super.update(conn, sql, id);
    }

    /**
     * 根据新闻id查询新闻
     *
     * @param id 被查询的新闻id
     * @return 被查询的新闻对象
     * @throws Exception
     */
    @Override
    public News getNewsById(Connection conn, Integer id) throws Exception {
        String sql = "SELECT * FROM easybuy_news WHERE id = ?";
        return getBean(conn, sql, id);
    }

    /**
     * 查询新闻列表
     *
     * @param params 封装查询参数
     * @return 查询到的对象列表
     * @throws Exception
     */
    @Override
    public List<News> getNewsList(Connection conn, NewsParams params) throws Exception {
        // ①、SQL语句
        String sql = "SELECT * FROM `easybuy_news` WHERE 1 = 1 ";

        // ②、保存拼接的参数
        List<Object> objParams = new ArrayList<>();

        // ③、需要根据params参数来拼接查询参数
        StringBuilder sb = new StringBuilder(sql);

        if (params != null) {
            // ④、开始判断：根据title进行模糊查询
            if (EmptyUtils.isNotEmpty(params.getTitle())) {
                sb.append(" AND `title` LIKE ? ");
                objParams.add(params.getTitle());
            }

            // 根据具体字段排序：这里使用默认排序，后面改成排序自定义
            if (EmptyUtils.isNotEmpty(params.getSort())) {
                sb.append(" ORDER BY `" + params.getSort() + "` ");
            }

            // 开启分页
            if (params.isPage()) {
                sb.append(" LIMIT " + params.getStartIndex() + "," + params.getPageSize());
            }
        }

        return getBeanList(conn, sb.toString(), objParams.toArray());
    }

    /**
     * 查询新闻的条数
     *
     * @param params 封装查询参数
     * @return 返回查询到的数据条数
     * @throws Exception
     */
    @Override
    public Integer getNewsCount(Connection conn, NewsParams params) throws Exception {
        // ①、SQL语句
        String sql = "SELECT COUNT(*) FROM easybuy_news WHERE 1 = 1 ";

        // ②、保存拼接的参数
        List<Object> objParams = new ArrayList<>();

        // ③、需要根据params参数来拼接查询参数
        StringBuilder sb = new StringBuilder(sql);

        if (params != null) {
            // ④、开始判断：根据title进行模糊查询
            if (EmptyUtils.isNotEmpty(params.getTitle())) {
                sb.append(" AND `title` LIKE ? ");
                objParams.add(params.getTitle());
            }
        }

        Long retVal = getValue(conn, sb.toString(), objParams.toArray());
        return retVal.intValue();
    }
}
